import redis

# 连接到本地Redis实例
# r = redis.Redis(host='127.0.0.1', port=6379, db=0, decode_responses=True)
r = redis.Redis(host='127.0.0.1', port=6379, db=0)
# r = redis.StrictRedis(host='localhost', port=6379, db=0)


def set_value(key, value):
    # 设置键值对
    r.set('key', 'value')

    # 获取键的值
    value = r.get('key')
    print(value)  # 输出 b'value'，注意返回的是字节字符串
    return value

#
# # 获取键的值并转换为字符串
# value = r.get('key').decode('utf-8')
# print(value)  # 输出 'value'
#
# # 删除键
# r.delete('key')
#
# # 检查键是否存在
# exists = r.exists('key')
# print(exists)  # 输出 True 或 False
#
# 列表操作
# r.rpush('mylist', 'item1')
# r.rpush('mylist', 'item2')
# items = r.lrange('mylist', 0, -1)
# for item in items:
#     print(item.decode('utf-8'))
# print(items)  # 输出 b'item1' 和 b'item2' 的字节字符串列表
# dic ={
#     "name":"zs",
#     "age":18,
#     "sex":"man"
# }

# r.hset("p_info","name",'zs')
# r.hset("p_info","age","18")
# r.hset("p_info","gender","m")
# r.hset("p_info","name","mm")
# r.hmset("f_info","a","b","s")
# print(r.hgetall("p_info"))
# print(r.hget("p_info","name").decode('utf-8'))


r.set('key3', 'value', ex=10)
r.setex('key5', 10, 'value')

# # 获取键的值
value = r.get('key3').decode('utf-8')
print(value)
# r.incr("key1", 1)
# print(r.get("key1").decode("utf"))
# print(value)


# 关闭连接
r.close()
